// packageA/pages/service-detail/index.js
const WXAPI = require("../../api/goods");
const WXAPI2 = require("../../api/api");

const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
import { handleResult } from "../../utils/tools";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgList:[],
    currentCarNum:0,
    activeClick: false,
    currentPreviewImg: "",
    showImage: false,
    goodsInfo: {},
    evaluateInfo: {},
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    currentId: "",
    background: ["demo-text-1", "demo-text-2", "demo-text-3"],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
  },
  toShopCarList(){
    wx.switchTab({
      url: '/pages/shop-cart/index',
    })
  },
  toBuy(){
    wx.navigateTo({
      url: `/packageA/pages/order-pay/index?id=${this.data.goodsInfo.sId}`,
    })
  },
   async addGoodsCar() {
     let isCustomLoading = true
     let data = [
      {
        sGoodsId: this.data.goodsInfo.sId,
        iGoodsCount:1
       }
     ]
    let res = await WXAPI.addGoodsInCar(data,isCustomLoading)
    if(res.data.code == 0) {
      wx.showToast({
        title: res.data.message,
      })
      this.setData({
        currentCarNum: Number(this.data.currentCarNum) + 1
      })
    }

  },
  async getCarNums(){
    let res = await WXAPI.getGoodsCarNum()
    if(res.data.code == 0) {
      this.setData({
        currentCarNum:res.data.data.total
      })
    }
  },
  toOrderService() {
    let data = {
      iPrice: this.data.goodsInfo.iPrice,
      sName: this.data.goodsInfo.sName,
      sContent: this.data.goodsInfo.sContent,
      sFileUrl: this.data.imgBaseUrl + this.data.goodsInfo.sFileUrl,
      sProjectId: this.data.goodsInfo.sId,
    };
    wx.navigateTo({
      url: `/packageA/pages/order-service/index?serviceObj=${JSON.stringify(
        data
      )}`,
    });
  },
  onClose() {
    this.setData({
      showImage: !this.data.showImage,
    });
  },

  view(e) {
    this.setData({
      currentPreviewImg: e.currentTarget.dataset.url,
      showImage: true,
    });
  },
  async getImagesList() {
    let res = await WXAPI2.getFiles({
      sBusinessId: this.data.currentId,
      sBusinessType: "OrgProductGood_Title",
      iFileFullUrl: 1,
    });
    if (res.data.code == 0) {
      this.setData({
        imgList: res.data.data,
      });
    }
  },
  async getDetail() {
    let res = await WXAPI.getGoodsDetail(this.data.currentId);
    if (res.data.code == 0) {
      this.getImagesList()
      res.data.data.sPublicityContent = unescape(res.data.data.sParameters);
      res.data.data.sIntroduce = unescape(res.data.data.sIntroduce);
      this.setData({
        goodsInfo: handleResult(res.data.data),
        currentCarNum: res.data.data.iGoodsShoppingCartCount == '--' ? '' : res.data.data.iGoodsShoppingCartCount 
      });
    }
  },
  async getEvaluate() {
    let params = {
      PageIndex: 1,
      PageSize: 1,
      iLoadImgUrl:1
    };
    let res = await WXAPI.getEvaluateListByGoods(this.data.currentId, params);
    if (res.data.code == 0) {
      this.setData({
        evaluateInfo: res.data.data.list ? res.data.data.list[0] : {},
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 存储服务项目id
    this.setData({
      currentId: options.id,
    });
    wx.setNavigationBarTitle({
      title: options.title,
    });
    // 获取购物车商品数量-接口变更
    // this.getCarNums()
    // 服务详情
    this.getDetail();
    // 服务评价
    this.getEvaluate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
